<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$fs = new functions;
$auth = Auth::getAuth('current_user');
$db = new Database();

if (isset($_POST)) {
    $location = $_FILES['fileupload']['tmp_name'];
    $formID = $_POST['FormID'];
    $formDoc = new Form($db, $formID);
    $return_array = import::importCSV($location);

//echo $location;
    $headers = array();
    $fieldValues = array();
    foreach ($return_array as $key => $value) {
        if ($key == 0) {
            array_push($headers, $value);
        } else {
            array_push($fieldValues, $value);
        }
    }

    $result = array();
    $date = $fs->currentDateTime();

    foreach ($fieldValues as $key => $value) {
        $result = null;
        foreach ($fieldValues[$key] as $key_1 => $value_1) {
            $result[$headers[0][$key_1]] = $fieldValues[$key][$key_1];
            $result['imported'] = '1';
            $result['Requestor'] = $auth['id'];
            $result['Processor'] = $auth['id'];
//            $result['Status'] = 'Draft';
            $workflow_object = functions::getFormActiveWorkflow($formDoc);
            $workflow_json = json_decode($workflow_object["json"], true);

            $result['Workflow_ID'] = $workflow_object["workflow_id"];
            $result['Node_ID'] = $workflow_json["workflow-default-action"];
            $result['DateCreated'] = $date;
        }

        $newRequest = new Request();
        $newRequest->load($formID, '0');
        $newRequest->data = $result;
        $newRequest->save();
        $newRequest->processWF();
        
        
    }
    if (($auth['user_level_id'] == 2 && ALLOW_USER_ADMIN == 1) || ($auth['user_level_id'] == 3 && ALLOW_USER_VIEW == 1)) {
        header('location:/user_view/application?id=' . $fs->base_encode_decode("encrypt", $formID));
    } else {
        header('location:/application?id=' . $fs->base_encode_decode("encrypt", $formID));
    }
}
?>
